package fr.xebia.monitoring;

import org.apache.log4j.Logger;

public class ActiveMonitor implements Monitor {

	private static final Logger logger = Logger.getLogger(ActiveMonitor.class);
	
	private MeasureHandler measureHandler;
	
	private String categoryName;
	private long startTime;
	private long endTime;

	public ActiveMonitor(String category) {
		super();
		this.categoryName = category;
	}

	public void setMeasureCollector(MeasureHandler measureHandler) {
		this.measureHandler = measureHandler;
	}

	public void start() {
		startTime = System.currentTimeMillis();
	}

	public void stop() {
		endTime = System.currentTimeMillis();
		if (MonitorFactory.isCategoryEnabled(categoryName) == false) {
			return ;
		}
		if (measureHandler == null) {
			logger.warn("Problem in MeasureHandler initialisation, measure not collected");
			return ;
		}
		measureHandler.addMeasure(categoryName, (endTime-startTime));
	}
	
	@Override
	public String toString() {
		return categoryName + " : "+(endTime-startTime)+" ms";
	}
}
